package org.jeecg.modules.zhoupackage.controller;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.zhouAdmin.admin.fabeicCenter.pojo.MlccFabricLadderPrice;
import org.jeecg.modules.zhouAdmin.admin.goodsCenter.util.ZXingutil;
import org.jeecg.modules.zhoupackage.pojo.CartEntity;
import org.jeecg.modules.zhoupackage.pojo.MlccCartInfo;
import org.jeecg.modules.zhoupackage.pojo.MlccFabricOrderinfo;
import org.jeecg.modules.zhoupackage.pojo.MlccStoreAddress;
import org.jeecg.modules.zhoupackage.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 购物车Controller
 *
 * @author zxl
 * @since 20200922
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    ICartService service;

    @Autowired
    ZXingutil zXingutil;


    /**
     * 面料添加购物车
     *
     * @param entity
     * @return
     */
    @RequestMapping("/insertCart")
    public JSONObject insertCart(MlccCartInfo entity, String token) {
        JSONObject jsonObject = new JSONObject();
        try {
            int result = service.insertCart(entity, token);
            if (result > 0) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "添加成功");
            } else if (result == -1) {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "token无效");
            } else if (result == -2) {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "库存不足");
            } else {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "添加失败");
            }
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "添加失败");
            return jsonObject;
        }
    }

    /**
     * 逻辑删除购物车
     *
     * @param id 购物车id
     * @return
     */
    @RequestMapping("/deleteCart")
    public JSONObject deleteCart(int id) {
        JSONObject jsonObject = new JSONObject();
        try {
            int result = service.removeCart(id);
            if (result > 0) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "删除成功");
            } else {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "删除失败");
            }
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "删除失败");
            return jsonObject;
        }
    }

    /**
     * 获取购物车列表
     *
     * @param token
     * @return
     */
    @RequestMapping("/getCartList")
    public JSONObject getCartList(String token) {
        JSONObject jsonObject = new JSONObject();
        try {
            List<CartEntity> result = service.getCartList(token);
            jsonObject.put("code", 0);
            jsonObject.put("data", result);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "查询失败");
            return jsonObject;
        }
    }

    /**
     * 购物车购买数量加一
     *
     * @param cartid
     * @param token  令牌
     */
    @RequestMapping("/incrByNum")
    public JSONObject incrByNum(int cartid, String token) {
        JSONObject jsonObject = new JSONObject();
        try {
            int result = service.incrByNum(cartid, token);
            if (result > 0) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "添加成功");
            } else if (result == -1) {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "库存不足，添加失败");
            }
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "添加失败");
            return jsonObject;
        }
    }

    /**
     * 购物车数量减一
     *
     * @param cartid 购物车ID
     * @param token  令牌
     */
    @RequestMapping("/decrByNum")
    public JSONObject decrByNum(int cartid, String token) {
        JSONObject jsonObject = new JSONObject();
        try {
            int result = service.decrByNum(cartid, token);
            if (result > 0) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "修改成功");
            } else if (result == -1) {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "修改失败，数量最小为1");
            }
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 1);
            jsonObject.put("msg", "修改失败");
            return jsonObject;
        }
    }

    /**
     * 购物车界面 修改面料购买米数
     *
     * @param cartid 购物车ID
     * @param buyNum 购买数量
     * @param token  令牌
     */
    @RequestMapping("/changeBuyNumber")
    public JSONObject changeBuyNumber(String cartid, Double buyNum, String token) {
        JSONObject jsonObject = new JSONObject();
        try {
            int result = service.changeBuyNumber(cartid, buyNum, token);
            if (result > 0) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "修改成功");
            } else if (result == -1) {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "修改失败，数量最小为1");
            } else if (result == -5) {
                jsonObject.put("code", 2);
                jsonObject.put("msg", "没有操作");
            }
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 3);
            jsonObject.put("msg", "修改失败");
            return jsonObject;
        }
    }


    /**
     * 购物车结算界面数据显示
     *
     * @param token
     * @param cartIds
     * @return
     */
    @RequestMapping("/settlement")
    public JSONObject settlement(String token, int[] cartIds) {
        JSONObject object1 = new JSONObject();
        try {
            JSONObject object = service.settlement(token, cartIds);
            if (null == object) {
                object1.put("code", 1);
                object1.put("msg", "token无效");
                return object1;
            }
            object.put("code", 0);
            object.put("msg", "查询成功");
            return object;
        } catch (Exception e) {
            e.printStackTrace();
            object1.put("code", 1);
            object1.put("msg", "查询失败");
            return object1;
        }
    }

    /**
     * 面料订单详情
     *
     * @param orderSn 面料订单号
     * @return
     */
    @RequestMapping("/getFabricOrderDetail")
    public JSONObject getFabricOrderDetail(String orderSn) {
        JSONObject object1 = new JSONObject();
        try {
            MlccFabricOrderinfo detail = service.getFabricOrderDetail(orderSn);
            object1.put("code", 0);
            object1.put("msg", "查询成功");
            object1.put("data", detail);
        } catch (Exception e) {
            e.printStackTrace();
            object1.put("code", 1);
            object1.put("msg", "查询失败");
        }
        return object1;
    }

    /**
     * 购物车更新备注
     *
     * @param id     购物车id
     * @param remark 备注
     */
    @RequestMapping("/updateRemark")
    public JSONObject updateRemark(String id, String remark) {
        JSONObject jsonObject = new JSONObject();
        try {
            int result = service.updateRemark(id, remark);
            if (result > 0) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "修改成功");
            } else if (result == -1) {
                jsonObject.put("code", 1);
                jsonObject.put("msg", "修改失败，数量最小为1");
            } else if (result == -5) {
                jsonObject.put("code", 2);
                jsonObject.put("msg", "没有操作");
            }
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", 3);
            jsonObject.put("msg", "修改失败");
        }
        return jsonObject;
    }

    /**
     * 获取面料的阶梯价格
     *
     * @param id 面料ID
     */
    @RequestMapping("/listFabricPrice")
    public JSONObject listFabricPrice(String id) {
        JSONObject object1 = new JSONObject();
        try {
            List<MlccFabricLadderPrice> ladderPrices = service.listFabricPrice(id);
            object1.put("code", 0);
            object1.put("msg", "查询成功");
            object1.put("data", ladderPrices);
        } catch (Exception e) {
            e.printStackTrace();
            object1.put("code", 1);
            object1.put("msg", "查询失败");
        }
        return object1;
    }

    /**
     * 根据门店标号获取门店地址
     *
     * @param storeCode 门店编号
     */
    @RequestMapping("/addressList")
    public JSONObject addressList(String storeCode) {
        JSONObject object1 = new JSONObject();
        try {
            List<MlccStoreAddress> addresses = service.addressList(storeCode);
            object1.put("code", 0);
            object1.put("msg", "查询成功");
            object1.put("data", addresses);
        } catch (Exception e) {
            e.printStackTrace();
            object1.put("code", 1);
            object1.put("msg", "查询失败");
        }
        return object1;
    }

    @RequestMapping("/getqrcode")
    public String test(String str) throws Exception {
        return zXingutil.encodeImg(str);
    }
}
